package com.boot2.core.utils;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author zhangweilin
 * @date 2018/6/12 15:12
 * @description: spring 工具类
 */
@Component
public class SpringUtil implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor {
//public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    private static AutowireCapableBeanFactory autowireCapableBeanFactory;
    private static BeanDefinitionRegistry registry;
    private static ConfigurableListableBeanFactory beanFactory;

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
        if (SpringUtil.autowireCapableBeanFactory == null) {
            SpringUtil.autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
        }

//        applicationContext.getAutowireCapableBeanFactory().applyBeanPostProcessorsAfterInitialization(, )
    }

    /**
     * 获取autowired工厂
     *
     * @return
     */
    public static AutowireCapableBeanFactory getAutowireCapableBeanFactory() {
        return autowireCapableBeanFactory;
    }

    /**
     * 注册一个现成的bean
     *
     * @param beanName
     * @param bean
     */
    public static <T> T regExistingBean(String beanName, T bean) {
//        T o = (T) autowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
        T o = (T) autowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(bean, beanName);
        return o;
    }

    //通过name获取 Bean.
    public static Object getBean(String name) {
        try {
            return applicationContext.getBean(name);
        } catch (BeansException e) {
            return null;
        }
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (BeansException e) {
//            e.printStackTrace();
            return null;
        }
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        boolean flag = applicationContext.containsBean(name);
        if (!flag) {
            return null;
        }
        return applicationContext.getBean(name, clazz);
    }

    /**
     * 动态注册一个bean
     *
     * @param beanName
     * @param rootBeanDefinition
     */
    public static void registerBeanDefinition(String beanName, RootBeanDefinition rootBeanDefinition) {
        registry.registerBeanDefinition(beanName, rootBeanDefinition);
    }

    /**
     * Modify the application context's internal bean definition registry after its
     * standard initialization. All regular bean definitions will have been loaded,
     * but no beans will have been instantiated yet. This allows for adding further
     * bean definitions before the next post-processing phase kicks in.
     *
     * @param registry the bean definition registry used by the application context
     * @throws BeansException in case of errors
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        SpringUtil.registry = registry;
    }

    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     *
     * @param beanFactory the bean factory used by the application context
     * @throws BeansException in case of errors
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtil.beanFactory = beanFactory;
    }

    /**
     * 返回动态注册bean的入口
     *
     * @return
     */
    public static BeanDefinitionRegistry getRegistry() {
        return SpringUtil.registry;
    }


    /**
     * 获取aop代理对象
     *
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }
}